package com.xiezc.service;

import com.xiaoleilu.hutool.cache.impl.TimedCache;
import com.xiezc.action.UploadAction;
import com.xiezc.entity.FileInfo;
import com.xiezc.entity.User;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Slf4j
@Data
@Component
public class UpFileRunnable implements Runnable {


    private List<FileInfo> fileInfos;

    @Resource
    private TimedCache timedCache;

    @Resource
    private UploadAction uploadAction;

    @Override
    public void run() {
        String ticket = (String) timedCache.get("ticket");
        User user = (User) timedCache.get("user");

        for (FileInfo fileInfo : fileInfos) {
            try {
                Path path = Paths.get(user.getPathSet() + "\\" + fileInfo.getFilePath() + "\\" + fileInfo.getFileName());
                uploadAction.upload(ticket, path.toFile());
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}
